package com.iraeta.service.iraorder.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.dao.iraorder.IraorderDao;
import com.iraeta.dao.iraorder.IraorderproductDao;
import com.iraeta.dao.iraorder.IraorderscstepDao;
import com.iraeta.dao.iraorder.IraorderwuliustepDao;
import com.iraeta.entity.IraOrderScStep;
import com.iraeta.entity.Iraorder;
import com.iraeta.entity.Iraorderproduct;
import com.iraeta.entity.resulttype.IraorderResultType;
import com.iraeta.service.iraorder.IraorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IraorderServiceImpl implements IraorderService {
    @Resource
    IraorderDao iraorderDao;
    @Resource
    IraorderscstepDao iraorderscstepDao;
    @Resource
    IraorderproductDao iraorderproductDao;

    @Override
    @Transactional
    public void addIraorder(Iraorder iraorder, JSONArray productJSONArray, JSONArray scStepJSONArray) {
        iraorderDao.insert(iraorder);
        for(int i=0;i<productJSONArray.size();i++){
            JSONObject productJSONObject = productJSONArray.getJSONObject(i);
            Iraorderproduct iraorderproduct = new Iraorderproduct();
            iraorderproduct.setProductname(StrUtil.isEmpty(productJSONObject.getString("productname"))?"produce name" : productJSONObject.getString("productname"));
            iraorderproduct.setProductguige(StrUtil.isEmpty(productJSONObject.getString("productguige"))?"produce specifications" : productJSONObject.getString("productguige"));
            iraorderproduct.setTotalcount(productJSONObject.getIntValue("totalcount") == 0 ? 1 : productJSONObject.getIntValue("totalcount"));
            iraorderproduct.setOrderid(iraorder.getId());
            this.iraorderproductDao.insert(iraorderproduct);
        }
        for(int i=0;i<scStepJSONArray.size();i++){
            JSONObject stepJSONObject = scStepJSONArray.getJSONObject(i);
            IraOrderScStep iraOrderScStep = new IraOrderScStep();
            iraOrderScStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
            iraOrderScStep.setOrderid(iraorder.getId());
            iraOrderScStep.setSortid(stepJSONObject.getIntValue("sortid"));
            this.iraorderscstepDao.insert(iraOrderScStep);
        }
//        for(int i=0;i<wlStepJSONArray.size();i++){
//            JSONObject stepJSONObject = wlStepJSONArray.getJSONObject(i);
//            IraOrderWuLiuStep iraOrderWuLiuStep = new IraOrderWuLiuStep();
//            iraOrderWuLiuStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
//            iraOrderWuLiuStep.setOrderid(iraorder.getId());
//            iraOrderWuLiuStep.setSortid(stepJSONObject.getIntValue("sortid"));
//            this.iraorderwuliustepDao.insert(iraOrderWuLiuStep);
//        }
    }

    @Override
    @Transactional
    public void updateIraorder(Iraorder iraorder,  JSONArray productJSONArray, JSONArray scStepJSONArray) {
        iraorderDao.updateByPrimaryKey(iraorder);
        for(int i=0;i<productJSONArray.size();i++){
            JSONObject productJSONObject = productJSONArray.getJSONObject(i);
            int id = productJSONObject.getIntValue("id");
            boolean isUpdateStep = id > 0;
            Iraorderproduct iraorderproduct = isUpdateStep ? iraorderproductDao.selectByPrimaryKey(id) : new Iraorderproduct();
            if(isUpdateStep){
                iraorderproduct.setProductname(StrUtil.isEmpty(productJSONObject.getString("productname"))?"produce name" : productJSONObject.getString("productname"));
                iraorderproduct.setProductguige(StrUtil.isEmpty(productJSONObject.getString("productguige"))?"produce specifications" : productJSONObject.getString("productguige"));
                iraorderproduct.setTotalcount(productJSONObject.getIntValue("totalcount") == 0 ? 1 : productJSONObject.getIntValue("totalcount"));
                this.iraorderproductDao.updateByPrimaryKey(iraorderproduct);
            }else{
                iraorderproduct.setProductname(StrUtil.isEmpty(productJSONObject.getString("productname"))?"produce name" : productJSONObject.getString("productname"));
                iraorderproduct.setProductguige(StrUtil.isEmpty(productJSONObject.getString("productguige"))?"produce specifications" : productJSONObject.getString("productguige"));
                iraorderproduct.setTotalcount(productJSONObject.getIntValue("totalcount") == 0 ? 1 : productJSONObject.getIntValue("totalcount"));
                iraorderproduct.setOrderid(iraorder.getId());
                this.iraorderproductDao.insert(iraorderproduct);
            }
        }
        for(int i=0;i<scStepJSONArray.size();i++){
            JSONObject stepJSONObject = scStepJSONArray.getJSONObject(i);
            int id = stepJSONObject.getIntValue("id");
            boolean isUpdateStep = id > 0;
            IraOrderScStep iraOrderScStep = isUpdateStep ? iraorderscstepDao.selectByPrimaryKey(id) : new IraOrderScStep();
            if(isUpdateStep){
                iraOrderScStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
                this.iraorderscstepDao.updateByPrimaryKey(iraOrderScStep);
            }else{
                iraOrderScStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
                iraOrderScStep.setOrderid(iraorder.getId());
                iraOrderScStep.setSortid(stepJSONObject.getIntValue("sortid"));
                this.iraorderscstepDao.insert(iraOrderScStep);
            }
        }
//        for(int i=0;i<wlStepJSONArray.size();i++){
//            JSONObject stepJSONObject = wlStepJSONArray.getJSONObject(i);
//            int id = stepJSONObject.getIntValue("id");
//            boolean isUpdateStep = id > 0;
//            IraOrderWuLiuStep iraOrderWuLiuStep = isUpdateStep ? iraorderwuliustepDao.selectByPrimaryKey(id) : new IraOrderWuLiuStep();
//            if(isUpdateStep){
//                iraOrderWuLiuStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
//                this.iraorderwuliustepDao.updateByPrimaryKey(iraOrderWuLiuStep);
//            }else{
//                iraOrderWuLiuStep.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
//                iraOrderWuLiuStep.setOrderid(iraorder.getId());
//                iraOrderWuLiuStep.setSortid(stepJSONObject.getIntValue("sortid"));
//                this.iraorderwuliustepDao.insert(iraOrderWuLiuStep);
//            }
//        }
    }

    @Override
    public void deleteIraorder(Integer id) {
        iraorderDao.deleteByPrimaryKey(id);
    }

    @Override
    public Iraorder getIraorder(Integer id) {
        return iraorderDao.selectByPrimaryKey(id);
    }

    @Override
    public IraorderResultType getIraorderResultType(Integer id) {
        return iraorderDao.selectIraorderResultType(id);
    }

    @Override
    public List<IraorderResultType> listIraorderByUserId(Integer createUserId, String orderno, String title) {
        return iraorderDao.selectByCreateUserId(createUserId, orderno, title);
    }

    @Override
    public List<IraorderResultType> listIraorderByKeHuId(Integer keHuUserId, String orderno, String title) {
        return iraorderDao.selectByKeHuUserId(keHuUserId, orderno, title);
    }
}
